package com.plian.system.service.yth.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.CollectionUtil;
import com.plian.system.constant.sys.FormTypeConstant;
import com.plian.system.entity.fr.indicators.YearGroupIndicatorsValue;
import com.plian.system.entity.fr.indicators.YearIndicators;
import com.plian.system.entity.fr.indicators.YearIndicatorsValue;
import com.plian.system.entity.yth.finance.YearlyDebt;
import com.plian.system.entity.yth.finance.YearlyDebtDetail;
import com.plian.system.mapper.yth.finance.YearlyDebtDetailMapper;
import com.plian.system.mapper.yth.finance.YearlyDebtMapper;
import com.plian.system.service.yth.IYearlyDebtService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 月度指标录入
 * @Author: jatpen
 * @Date: 2023/5/9 2:33 PM
 */
@Slf4j
@Service
public class YearlyDebtServiceImpl extends ServiceImpl<YearlyDebtMapper, YearlyDebt> implements IYearlyDebtService {

    private final String YTH_DATASOURCE_TYPE = "3";

    @Autowired
    private YearlyDebtDetailMapper debtDetailMapper;


    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
//    @DS("slave_1")
    @Override
    public List<YearIndicators> getSyncData(Date startTime, Date endTime) {
        List<YearIndicators> yearIndicatorsList = new ArrayList<>();
        List<YearlyDebt> yearlyDebts = list(new LambdaQueryWrapper<YearlyDebt>()
                .gt(Optional.ofNullable(startTime).isPresent(), YearlyDebt::getCreateTime, startTime)
                .le(Optional.ofNullable(endTime).isPresent(), YearlyDebt::getCreateTime, endTime));
        if (CollectionUtil.isNotEmpty(yearlyDebts)) {
            yearIndicatorsList = conversion(yearlyDebts);
        }
        return yearIndicatorsList;
    }

    /**
     * 转换对象
     *
     * @param yearlyDebts
     * @return
     */
    private List<YearIndicators> conversion(List<YearlyDebt> yearlyDebts) {

        List<YearIndicators> yearIndicatorsList = new ArrayList<>();

        List<YearlyDebtDetail> yearlyDebtDetails = debtDetailMapper.selectList(new LambdaQueryWrapper<YearlyDebtDetail>().in(YearlyDebtDetail::getDebtId,
                yearlyDebts.stream().map(YearlyDebt::getId).collect(Collectors.toList())));

        for (YearlyDebt yearlyDebt : yearlyDebts) {
            YearIndicators yearIndicators = new YearIndicators();
            yearIndicators.setOrgId(yearlyDebt.getReportOrgId().toString());
            yearIndicators.setAssigning(Objects.toString(yearlyDebt.getCompanyLevel(), null));
            yearIndicators.setFyear(Objects.toString(yearlyDebt.getYear(), null));
            yearIndicators.setStatus("0");
            yearIndicators.setCode(yearlyDebt.getNumber());
            yearIndicators.setCreateUser(Objects.toString(yearlyDebt.getReportUserId(), null));
            yearIndicators.setCreateTime(new Date());
            //本级指标
            List<YearlyDebtDetail> debtDetails = yearlyDebtDetails.stream().filter(e -> e.getType() == 1 && e.getDebtId().equals(yearlyDebt.getId())).collect(Collectors.toList());
            List<YearIndicatorsValue> indicatorsValues = new ArrayList<>();
            debtDetails.forEach(item -> {
                YearIndicatorsValue yearIndicatorsValue = new YearIndicatorsValue();
                yearIndicatorsValue.setYearIndicatorsId(yearlyDebt.getId().toString());
                yearIndicatorsValue.setIndicatorsName(transIndicatorsName(item.getIndexType()));
                yearIndicatorsValue.setWarningLine(item.getWarnLine() == null ? null : item.getWarnLine().toString());
                yearIndicatorsValue.setRegulatoryLine(item.getSuperviseLine() == null ? null : item.getSuperviseLine().toString());
                yearIndicatorsValue.setAverage(item.getAvgValue() == null ? null : item.getAvgValue().toString());
                yearIndicatorsValue.setDataSource(YTH_DATASOURCE_TYPE);
                yearIndicatorsValue.setDataSourceId(item.getId().toString());
                indicatorsValues.add(yearIndicatorsValue);
            });
            //组合指标
            List<YearlyDebtDetail> groupDebtDetails = yearlyDebtDetails.stream().filter(e -> e.getType() == 2 && e.getDebtId().equals(yearlyDebt.getId())).collect(Collectors.toList());
            List<YearGroupIndicatorsValue> groupIndicatorsValueArrayList = new ArrayList<>();
            groupDebtDetails.forEach(item -> {
                YearGroupIndicatorsValue groupIndicatorsValue = new YearGroupIndicatorsValue();
                groupIndicatorsValue.setYearIndicatorsId(yearlyDebt.getId().toString());
                groupIndicatorsValue.setIndicatorsName(transIndicatorsName(item.getIndexType()));
                groupIndicatorsValue.setWarningLine(item.getWarnLine() == null ? null : item.getWarnLine().toString());
                groupIndicatorsValue.setRegulatoryLine(item.getSuperviseLine() == null ? null : item.getSuperviseLine().toString());
                groupIndicatorsValue.setAverage(item.getAvgValue() == null ? null : item.getAvgValue().toString());
                groupIndicatorsValue.setDataSource(YTH_DATASOURCE_TYPE);
                groupIndicatorsValue.setDataSourceId(item.getId().toString());
                groupIndicatorsValueArrayList.add(groupIndicatorsValue);
            });
            yearIndicators.setIndicatorsValues(indicatorsValues);
            yearIndicators.setGroupIndicatorsValues(groupIndicatorsValueArrayList);
            yearIndicators.setFormType(String.valueOf(FormTypeConstant.FR_YEARINDICATORS));
            yearIndicators.setDataSource(YTH_DATASOURCE_TYPE);
            yearIndicators.setDataSourceId(yearlyDebt.getId().toString());
            yearIndicatorsList.add(yearIndicators);
        }
        return yearIndicatorsList;
    }


    /**
     * 转换指标名称
     *
     * @param indexType
     * @return
     */
    private String transIndicatorsName(Integer indexType) {
        switch (indexType) {
            case 1:
                return "资产负债率";
            case 2:
                return "已获利息倍数";
            case 3:
                return "带息负债比率";
            case 4:
                return "现金流动负债比率";
            default:
                return null;

        }
    }
}
